import { _decorator, CircleCollider2D, Collider2D, Component, Contact2DType, Director, director, ERigidBody2DType, IPhysics2DContact, Label, PhysicsSystem2D, RigidBody2D, } from 'cc';
import HeChengDaXiGua_GameManager from './HeChengDaXiGua_GameManager';
const { ccclass, property } = _decorator;

@ccclass('HeChengDaXiGuaFruit')
export default class HeChengDaXiGua_Fruit extends Component {
    @property
    level: number = 1;
    isMerged: boolean = false;    // 标记是否已合成，避免重复合成
    isLanded: boolean = false;    // 标记是否已落地
    public gameManager: HeChengDaXiGua_GameManager = null;
    public collider: CircleCollider2D | null = null;
    public Label_id: Label | null = null;
    onLoad() {
        PhysicsSystem2D.instance.enable = true;

        this.collider = this.getComponent(CircleCollider2D);
        if (this.collider) {
            this.collider.friction = 0.1; // 0.1;      // 摩擦力，适中
            this.collider.restitution = 0.3;  // 弹性，保证碰撞弹跳
            this.collider.density = this.getDensityByLevel(this.level);  // 根据等级设密度

            this.collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
        }
        this.Label_id = this.getComponentInChildren(Label);
        if (this.Label_id) {
            this.Label_id.node.active = false;
        }

        // PhysicsSystem2D.instance.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);

    }
    getDensityByLevel(level: number): number {
        switch (level) {
            case 1: return 1;
            case 2: return 1.01;
            case 3: return 1.02;
            case 4: return 1.03;
            case 5: return 1.04;
            default: return 1.05; // 6级及以上

            // case 1: return 2.5;
            // case 2: return 3.5;
            // case 3: return 4.5;
            // case 4: return 6.0;
            // case 5: return 7.5;
            // default: return 9.0; // 6级及以上
        }
    }

    setGameManager(manager: HeChengDaXiGua_GameManager) {
        this.gameManager = manager;
    }

    // onBeginContact(contact: PhysicsContact, selfCollider: PhysicsCollider, otherCollider: PhysicsCollider) {
    // onBeginContact(contact: IPhysics2DContact, selfCollider: Collider2D, otherCollider: Collider2D) {
    async onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        // return

        // console.log(`onBeginContact`)
        // console.log(`selfCollider`, selfCollider);
        // console.log(`otherCollider`, otherCollider);

        const otherNode = otherCollider.node;
        const otherFruit = otherNode.getComponent(HeChengDaXiGua_Fruit);

        // console.log(`otherCollider`, otherCollider);

        // return
        // if (!otherNode) {
        //     return
        // }

        // console.log(`otherFruit-----------`, otherFruit);

        if (otherFruit) {
            // 两个水果合并
            if (this.isMerged || otherFruit.isMerged) return;

            if (otherFruit.level === this.level && this.gameManager) {
                // 标记双方已合并，防止重复调用
                this.isMerged = true;
                otherFruit.isMerged = true;


                await new Promise<void>((resolve) => {
                    director.once(Director.EVENT_AFTER_PHYSICS, resolve);
                });

                // director.once(Director.EVENT_AFTER_PHYSICS, () => {
                this.gameManager.mergeFruits(this, otherFruit);
                // }, this)
            }
        } else {
            // 碰到了非水果的物体，比如地板
            const rigid = otherNode.getComponent(RigidBody2D);
            if (rigid && rigid.type === ERigidBody2DType.Static) {
                this.isLanded = true;
            }
        }
    }
}